package work.xiaohong.account.controller;

import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import work.xiaohong.account.entity.AccountTbl;
import work.xiaohong.account.service.AccountTblService;
import work.xiaohong.model.Result;
import work.xiaohong.model.StatusCode;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Tag(name = "AccountTbl接口文档")
@RestController
@RequestMapping("/account")
@CrossOrigin
public class AccountTblController {

    @Autowired
    private AccountTblService accountTblService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Operation(security ={@SecurityRequirement(name ="Authorization")})
    @RequestMapping("/test/date")
    public Result testDate(){
        return new Result(true, StatusCode.OK.getCode(),"查询成功",new Date()) ;
    }

    @Operation(security ={@SecurityRequirement(name ="Authorization")})
    @RequestMapping("/test/redis")
    public Result testRedis(){
        AccountTbl accountTbl = new AccountTbl();
        accountTbl.setId(1);
        accountTbl.setMoney(10000);
        redisTemplate.opsForValue().set("account",accountTbl);

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(redisTemplate.opsForValue().get("account"));

        return new Result(true, StatusCode.OK.getCode(),"查询成功",new Date()) ;
    }


    /***
     * 条件分页查询
     * @param accountTbl
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "AccountTbl对象",required = true) AccountTbl accountTbl, @PathVariable  int page, @PathVariable  int size){
        //调用AccountTblService实现分页条件查询AccountTbl
        Page pageInfo = accountTblService.findPage(accountTbl, page, size);
        return new Result<Page>(true, StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
     * 分页查询
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用AccountTblService实现分页查询AccountTbl
        Page pageInfo = accountTblService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
     * 条件查询
     * @param accountTbl
     * @return
     */
    @Operation(summary = "条件查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping(value = "/search" )
    public Result<List<AccountTbl>> findList(@RequestBody(required = false) @Parameter(name = "AccountTbl对象",required = true) AccountTbl accountTbl){
        //调用AccountTblService实现条件查询AccountTbl
        List<AccountTbl> list = accountTblService.findList(accountTbl);
        return new Result<List<AccountTbl>>(true,StatusCode.OK.getCode(),"查询成功",list);
    }

    /***
     * 根据Id删除accountTbl数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除AccountTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "删除对象ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用AccountTblService实现根据ID删除
        accountTblService.delete(id);
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }

    /***
     * 根据Id批量删除accountTbl数据
     * @param ids
     * @return
     */
    @Operation(summary = "根据Id批量删除AccountTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @DeleteMapping
    public Result deleteByIds(@RequestBody @Parameter(name = "需要删除的AccountTbl对象id集合",required = true) Long[] ids){
        //调用AccountTblService实现根据ID批量删除
        accountTblService.deleteByIds(Arrays.asList(ids));
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }


    /***
     * 根据ID修改AccountTbl数据
     * @param accountTbl
     * @return
     */
    @Operation(summary = "根据ID修改AccountTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PutMapping
    public Result updateById(@RequestBody @Parameter(name = "AccountTbl对象",required = true) AccountTbl accountTbl){
        //调用AccountTblService实现修改AccountTbl
        int i = 1/0;
        accountTblService.updateById(accountTbl);
        return new Result(true,StatusCode.OK.getCode(),"修改成功");
    }

    /***
     * 新增AccountTbl数据
     * @param accountTbl
     * @return
     */
    @Operation(summary = "添加AccountTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "AccountTbl对象",required = true) AccountTbl accountTbl){
        //调用AccountTblService实现添加AccountTbl
        accountTblService.add(accountTbl);
        return new Result(true,StatusCode.OK.getCode(),"添加成功");
    }

    /***
     * 根据ID查询AccountTbl数据
     * @param id
     * @return
     */
    // 第一个参数一般以类名称取名 第二个是key el表达式  第三个是缓存类型。both表示本地和redis都缓存 第三个是过期时间
    @Cached(name = "work.xiaohong.account.controller.AccountTblController:",key = "#id",cacheType = CacheType.REMOTE,expire = 1200)
    @Operation(summary = "根据ID查询AccountTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
    @GetMapping("/{id}")
    public Result<AccountTbl> findById(@PathVariable Integer id){
        //调用AccountTblService实现根据ID查询AccountTbl
        AccountTbl accountTbl = accountTblService.findById(id);
        return new Result<AccountTbl>(true,StatusCode.OK.getCode(),"查询成功",accountTbl);
    }

    /***
     * 查询AccountTbl全部数据
     * @return
     */
    @Operation(summary = "查询所有AccountTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @GetMapping
    public Result<List<AccountTbl>> findAll(){
        //调用AccountTblService实现查询所有AccountTbl
        List<AccountTbl> list = accountTblService.findAll();
        return new Result<List<AccountTbl>>(true, StatusCode.OK.getCode(),"查询成功",list) ;
    }


    /***
     * 查询AccountTbl全部数据
     * @return
     */
    @Operation(summary = "查询所有AccountTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping("/pay")
    public Result<List<AccountTbl>> pay(){
        //调用AccountTblService实现查询所有AccountTbl
        List<AccountTbl> list = accountTblService.findAll();
        return new Result<List<AccountTbl>>(true, StatusCode.OK.getCode(),"查询成功",list) ;
    }

}
